#include <stdlib.h>
#include <string.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
int main(int argc, char **argv)
{
WORD RequiredVersion;
WSADATA WData;
SOCKET ss, s;
struct sockaddr_in addr, incoming;
int alen;
long val;
if (argc<2) {
printf("podaj numer portu jako parametr\n");
return 1;
}
RequiredVersion = MAKEWORD(2, 0);
if (WSAStartup(RequiredVersion, &WData) != 0) {
printf("Blad inicjalizacji WinSock2\n");
return 1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[1]));
addr.sin_addr.s_addr = INADDR_ANY;
ss = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
bind(ss,(struct sockaddr*) &addr, sizeof(addr));
listen(ss, 10);
alen = sizeof(incoming);
while((s = accept(ss, (struct sockaddr*) &incoming, &alen))
!= INVALID_SOCKET) {
recv(s, (char*) &val, sizeof(long), 0);
val = ntohl(val);
printf("Otrzymano %d od %s:%u\n",
val, inet_ntoa(incoming.sin_addr),
ntohs(incoming.sin_port));
if (val == 0) break;
val++;
val = htonl(val);
send(s, (char*) &val, sizeof(long), 0);
closesocket(s);
}
closesocket(ss);
WSACleanup();
return 0;
}